package com.example.zhc36.fragment.frag;


import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.example.zhc36.fragment.R;
import com.example.zhc36.fragment.apadter.Pageradapter1;
import com.example.zhc36.fragment.more.MainBookFrament;
import com.example.zhc36.fragment.more.MainDeviceFrament;
import com.example.zhc36.fragment.more.MainFridendFrament;
import com.example.zhc36.fragment.more.MainGroupFrament;
import com.example.zhc36.fragment.more.MainpublicFrament;
import com.example.zhc36.fragment.utils.app.BaseFragment;


public class MainlxrFragment extends BaseFragment {
    private ViewPager vp;
    private TextView t2;
    private TabLayout t1;

    private BaseFragment[] frags;
    private String[] texts;



    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.frag_main_lxr_news, container, false);
        vp = view.findViewById(R.id.vp);
        t1 = view.findViewById(R.id.t1);

        return view;
    }


    @Override
    protected void initData() {


        t1.setSelectedTabIndicatorColor(Color.BLACK);
        t1.setSelectedTabIndicatorHeight(8);
        t1.setTabMode(TabLayout.MODE_SCROLLABLE);
        frags = new BaseFragment[]{new MainFridendFrament(), new MainGroupFrament(), new MainDeviceFrament(), new MainBookFrament(),
                new MainpublicFrament()};
        texts = new String[]{"好友", "群聊", "设备", "通讯录", "公众号", "公众号", "公众号"};
        for (int i = 0; i < texts.length; i++) {
            t1.addTab(t1.newTab().setText(texts[i]));


        }
        Pageradapter1 pagerAdapter = new Pageradapter1(getChildFragmentManager(), frags, texts);

        vp.setAdapter(pagerAdapter);
        t1.setupWithViewPager(vp, true);


//        t1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                int position = tab.getPosition();
//                if(vp.getCurrentItem()!=position){
//                vp.setCurrentItem(position,true);
//            }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
//  vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
//      @Override
//      public void onPageSelected(int position) {
//          if(position!=t1.getSelectedTabPosition()){
//              t1.getTabAt(position).select();
//
//          }
//      }
//  });

//////////控制切换速度.
//        RestScor mScroller = null;

//        Field mField = null;
//        try {
//            mField = ViewPager.class.getDeclaredField("mScroller");
//            mField.setAccessible(true);
//            mScroller = new RestScroller(vp.getContext());
//            mField.set(vp, mScroller);
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//        //控制速度 秒数*1000=实际切换秒数
//        mScroller.setmDuration(3 * 1000);
    }

    @Override
    protected void initListener() {
        vp.setPageTransformer(true, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                if (position >= -1 && position <= 1) {
                    page.setPivotX(position < 0 ? page.getWidth() : 0);
                    page.setRotationY(90 * position);



                }


            }
        });



    }




}
